﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Interactivity;

using NetBay.Core.BusinessEntities.Versioning;

namespace Rio.Manager.Application.Behaviors
{
    public class EnabledDisableComboModelBehavior : Behavior<ComboBox>
    {
        #region Fields

        public static readonly DependencyProperty SelectedVersionTypeProperty = 
            DependencyProperty.Register("SelectedVersionType", typeof(VersionType), typeof(EnabledDisableComboModelBehavior), null);

        #endregion Fields

        #region Constructors

        public EnabledDisableComboModelBehavior()
            : base()
        {
        }

        #endregion Constructors

        #region Properties

        public VersionType SelectedVersionType
        {
            get { return (VersionType)GetValue(SelectedVersionTypeProperty); }
            set { SetValue(SelectedVersionTypeProperty, value); }
        }

        #endregion Properties

        #region Methods

        protected override void OnAttached()
        {
            base.OnAttached();
            this.AssociatedObject.SelectionChanged += SelectionChanged;
        }

        protected override void OnDetaching()
        {
            base.OnDetaching();
            this.AssociatedObject.SelectionChanged -= SelectionChanged;
        }

        private void SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (SelectedVersionType == VersionType.None)
                this.AssociatedObject.SelectedItem = null;
        }

        #endregion Methods
    }
}